import { oauth2TokenMock } from './data/token'
import { getNowDateTime } from '../../utils'
let oauth2TokenList = oauth2TokenMock(400)
export default [
  {
    url: RegExp('/system/oauth2-token/delete' + '.*'), // 删除
    method: 'delete',
    response: params => {
      deleteByAccessToken(params)
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: null
      }
    }
  },
  {
    url: RegExp('/system/oauth2-token/page' + '.*'), // 查询分页列表
    method: 'get',
    response: params => {
      return {
        code: 200,
        status: true,
        msg: '成功！',
        data: getListByPage(params)
      }
    }
  }
]

const getListByPage = params => {
  let result = oauth2TokenList
  result = result.filter(item => !item.deleted)
  if (params.query.userId) {
    result = result.filter(item => item.userId === params.query.userId)
  }
  if (params.query.userType) {
    result = result.filter(item => item.userType === parseInt(params.query.userType))
  }
  if (params.query.clientId) {
    result = result.filter(item => item.clientId === params.query.clientId)
  }
  return {
    list: result.slice(parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1), parseInt(params.query.pageSize) * (parseInt(params.query.pageIndex) - 1) + parseInt(params.query.pageSize)),
    total: result.length
  }
}

const deleteByAccessToken = params => {
  oauth2TokenList.forEach(item => {
    if (item.accessToken === params.query.accessToken) {
      item.deleted = true
      item.updater = '1'
      item.update_time = getNowDateTime()
      item.deleted_time = getNowDateTime()
    }
  })
}
